/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ccsoft.mc.core.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.ccsoft.mc.core.Reference;
import org.ccsoft.mc.core.Service;
import org.ccsoft.mc.core.ServiceBundle;
import org.ccsoft.mc.core.ServiceChangeListener;
import org.ccsoft.mc.core.ServiceContext;
import org.ccsoft.mc.core.ServiceContextAware;
import org.ccsoft.mc.core.exceptions.SetRefereceException;
/**
 * 
 * @author Chao Cai
 *
 * resolve the dependency between the components
 */
public class DependencyManager implements ServiceChangeListener,ServiceContextAware{
	private ServiceContext context;
	
	
	public void onServiceReady(ServiceBundle instance) {
		Set<ServiceBundle> bundles=new HashSet<ServiceBundle>();
		BaseCache unResolvedCache=context.getUnResolvingServiceCache();
		BaseCache readyCache=context.getReadyServiceCache();
		List<Service> services=instance.getServices();
		for (Service service:services){
			bundles.addAll(unResolvedCache.findBundlesByReference(service.getInterfaceName()));
		}
		for (ServiceBundle bundle:bundles){
			if(BundleUtil.isAllReferenceAvailable(bundle,readyCache)){
				BundleUtil.injectReference(bundle,readyCache);
			}
		}
	}

	public void setServiceContext(ServiceContext context) {
		this.context=context;
		
	}
	
}
